package com.qyer.search.model.lastminute;

import static com.qyer.commons.utils.CommonUtils.cast2Int;
import static com.qyer.commons.utils.CommonUtils.cast2String;

import com.qyer.search.Constants;
import com.qyer.search.cache.mutable.MutableCaches;
import com.qyer.search.enums.TagStatus;
import com.qyer.search.enums.TagType;
import com.qyer.search.filter.AbstractFilter;
import com.qyer.search.filter.AbstractValidator;
import com.qyer.search.model.SearchParameterEntity;
import com.qyer.search.model.aggregation.AggregationProduct;
import com.qyer.search.model.aggregation.AggregationRegular;
import com.qyer.search.model.aggregation.City;
import com.qyer.search.model.aggregation.Country;
import com.qyer.search.model.aggregation.Poi;
import com.qyer.search.model.aggregation.STGInfo;
import com.qyer.search.model.aggregation.SetOutPlace;
import com.qyer.search.model.aggregation.Topic;
import com.qyer.search.model.aggregation.TravelDay;
import com.qyer.search.model.aggregation.VStype;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.collections4.MapUtils;
import org.apache.commons.lang3.StringUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * Created by tangzheng on 16/8/3.
 */
public class LightLessZSKU extends ZSKU{
  private  int id;
  private Set<Integer> type1 = new HashSet<>();
  private Set<Integer> type2 = new HashSet<>();
  private int market_info;
  private String title;

  public LightLessZSKU(int id) {
    this.id = id;
  }

  public LightLessZSKU() {
  }

  @Override public String getTitle() {
    return title;
  }

  public void setTitle(String title) {
    this.title = title;
  }

  public LightLessZSKU type1(Set<Integer> type1) {
    setType1(type1);
    return this;
  }

  public LightLessZSKU type1(int t) {
    if(CollectionUtils.isEmpty(type1)) {
      setType1(new HashSet<Integer>());
      addType1(t);
    }else {
      addType1(t);
    }
    return this;
  }

  public LightLessZSKU type2(int t) {
    if(CollectionUtils.isEmpty(type2)) {
      setType2(new HashSet<Integer>());
      addType2(t);
    }else {
      addType2(t);
    }
    return this;
  }

  public LightLessZSKU type2(Set<Integer> type2) {
    setType2(type2);
    return this;
  }

  public int getId() {
    return id;
  }

  public void setId(int id) {
    this.id = id;
  }

  public Set<Integer> getType1() {
    return type1;
  }

  public void setType1(Set<Integer> type1) {
    this.type1 = type1;
  }

  public Set<Integer> getType2() {
    return type2;
  }

  public void setType2(Set<Integer> type2) {
    this.type2 = type2;
  }

  public int getMarket_info() {
    return market_info;
  }

  public void setMarket_info(int market_info) {
    this.market_info = market_info;
  }

  public void addType1 (int t){
    this.type1.add(t);
  }

  public void addType2 (int t){
    this.type2.add(t);
  }


  private static LightLessZSKU build(int id) {
    return new LightLessZSKU(id);
  }

  public static LightLessZSKU build(SearchParameterEntity spe, Map<String, Object> dataMap,
                                List<AbstractFilter> filters, AggregationRegular regular,
                                Set<Integer> stgSet){
    if (MapUtils.isEmpty(dataMap)) {
      return null;
    }
    int id = cast2Int(dataMap.get(FIELD_ID));
    LightLessZSKU lightZSKU = build(id);
    AbstractValidator validator = MutableCaches.getInstance().SKU_AVAILABILITY_VALIDATOR
      .getCacheContent();
    if (!validator.isValid(spe, id)) {
      return null;
    }
    if(filters != null) {
      for (int i = 0; i < filters.size(); i++) {
        if (!filters.get(i).isValid(dataMap, spe))
          return null;
      }
    }
    int market_info = Integer.MAX_VALUE;
    Map<Integer, Set<Integer>> vTypeMap = regular.getvTypeMap();

    lightZSKU.setMarket_info(market_info);
    //String ztitle = cast2String(dataMap.get(FIELD_TITLE));
    lightZSKU.setTitle("商品名未显示");

    //
    int channel = regular.getChannel();
    Set<Integer> stypeChannel = MutableCaches.getInstance().Z_TAG_SNAPSHOT.getCacheContent()
                                                                          .getvStypeMap().get(channel).getS_type_set();
    Set<Integer> vtypeSet = MutableCaches.getInstance().Z_TAG_SNAPSHOT.getCacheContent()
                                                                      .getvStypeMap().get(channel).getS_type_set();
    Map<Integer,List<Integer>> neMap = MutableCaches.getInstance().Z_TAG_SNAPSHOT
      .getCacheContent().getvStypeMap().get(channel).getNegativeMap();
    Set<Integer> market_tag_Set = MutableCaches.getInstance().Z_TAG_SNAPSHOT
      .getCacheContent().getvStypeMap().get(channel).getMarket_tag_Set();

    //获取styoe

    ZTag zTag;
    List<Map<String, Object>> listedData = (List<Map<String, Object>>) dataMap.get(FIELD_Z_TAGS);
    try {
      if (CollectionUtils.isNotEmpty(listedData)) {
        for (Map<String, Object> map : listedData) {
          zTag = ZTag.buildTag(map);
          if (zTag == null) {
            continue;
          }
          if (zTag.getChannel() != channel) {
            if (zTag.getChannel() != -1) {
              continue;
            }
          }
          String tag_name = zTag.getTag_name();
          int tid = zTag.getTid();
          int zchannel = zTag.getChannel();


          //stg
          if (zchannel > 0) {
            if (!vTypeMap.containsKey(tid)) {
              continue;
            } else {
              if(!lightZSKU.getType1().contains(tid)){
                lightZSKU.type1(tid);
                stgSet.add(tid);
              }
            }
          } else {
            //非stg
            if (vtypeSet.contains(tid)) {
              List<Integer> list = neMap.get(tid);
              for (int gt : list) {
                if (vTypeMap.containsKey(gt)) {
                  Set<Integer> set = vTypeMap.get(gt);
                  if (set.contains(tid)) {
                    lightZSKU.type2(tid);
                  }
                }
              }
            } else {
              //运营
              if (market_tag_Set.contains(tid)) {
                lightZSKU.setMarket_info(Integer.valueOf(tag_name.trim()));
                //System.out.println("运营:" + lightZSKU.id + ":级别:" + lightZSKU.getMarket_info());
              }
            }
          }

        }
      } else {
        return null;
      }
    }catch (Exception e){
      e.printStackTrace();
    }
    ///test
    /*try {
      List<Map<String, Object>> idsData = (List<Map<String, Object>>) dataMap.get(FIELD_ZTAG_IDS);
      for (Map<String, Object> a : idsData) {
        System.out.println("ztagids:"+a);
      }
    }catch (Exception e){
      e.printStackTrace();
    }*/
    ///
    return lightZSKU;
  }

  public List<SimpleVSKU> buildVSKUS(){
    List<SimpleVSKU> simpleVSKUs = new ArrayList<>(this.type1.size());
    for(int t1:this.type1){
      simpleVSKUs.add(new SimpleVSKU(this.id, t1, 0, this.type2, market_info, title, new ArrayList<Tag>(), new ArrayList<String>()));
    }
    if(CollectionUtils.isEmpty(simpleVSKUs))
      System.out.println("buildLessVSKUS:"+this.id);
    return simpleVSKUs;
  }
}
